package com.baizhi.cmfzmanager.controller;

import com.baizhi.cmfzmanager.entity.Admin;
import com.baizhi.cmfzmanager.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;
    @RequestMapping("/login")
    public String login(String username, String password, String code, HttpSession session){
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        Subject subject= SecurityUtils.getSubject();
        try {
            subject.login(token);
        } catch (Exception e) {
            return "redirect:/login.jsp";
        }
        return "redirect:/main/main.jsp";

    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login.jsp";
    }

    @RequestMapping("regist")
    public String regist(String username, String password){
        adminService.regist(username,password);
        return "redirect:/login.jsp";
    }
}
